package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BEleEmission;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BEleEmissionMapper;
import com.isdepci.project.carbon.service.IBEleEmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 电力排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BEleEmissionServiceImpl implements IBEleEmissionService
{
    @Autowired
    private BEleEmissionMapper bEleEmissionMapper;


    @Override
    public AjaxResult calEleEmission(Long projectId) {
        // 查询出所有相关数据
        List<BEleEmission> bEleEmissions = bEleEmissionMapper.selectEleEmissionListByProjectId(projectId);
        BigDecimal totalEmissions = BigDecimal.ZERO;
        // 遍历每一条数据，计算ccerVolume和carbonVolume的总和
        for (BEleEmission ele : bEleEmissions) {
            BigDecimal buyEle = ele.getBuyEle() == null ? BigDecimal.ZERO :  ele.getBuyEle();
            BigDecimal eleEmissionFactor = ele.getEleEmissionFactor() == null ? BigDecimal.ZERO : ele.getEleEmissionFactor();
            BigDecimal tradeVolume = buyEle.multiply(eleEmissionFactor);
            ele.setCarbonEmissions(tradeVolume);
            bEleEmissionMapper.updateBEleEmission(ele);
            // 累加到总和
            totalEmissions = totalEmissions.add(tradeVolume);
        }
        return AjaxResult.success(totalEmissions.setScale(2, RoundingMode.HALF_UP));
    }


    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BEleEmission> list =  excel.getEleEmissionList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bEleEmissionMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询电力排放
     */
    @Override
    public BEleEmission selectBEleEmissionById(Long id)
    {
        return bEleEmissionMapper.selectBEleEmissionById(id);
    }

    /**
     * 查询电力排放列表
     */
    @Override
    public List<BEleEmission> selectBEleEmissionList(BEleEmission bEleEmission)
    {
        return bEleEmissionMapper.selectBEleEmissionList(bEleEmission);
    }

    /**
     * 新增电力排放
     */
    @Override
    public int insertBEleEmission(BEleEmission bEleEmission)
    {
        return bEleEmissionMapper.insertBEleEmission(bEleEmission);
    }

    /**
     * 修改电力排放
     */
    @Override
    public int updateBEleEmission(BEleEmission bEleEmission)
    {
        return bEleEmissionMapper.updateBEleEmission(bEleEmission);
    }

    /**
     * 批量删除电力排放
     */
    @Override
    public int deleteBEleEmissionByIds(Long[] ids)
    {
        return bEleEmissionMapper.deleteBEleEmissionByIds(ids);
    }

    /**
     * 删除电力排放信息
     */
    @Override
    public int deleteBEleEmissionById(Long id)
    {
        return bEleEmissionMapper.deleteBEleEmissionById(id);
    }


}
